package projectServlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Testing.NewLeaveList;

import dao.DAOException;
import dao.LeaveDAO;
import dao.mysql.LeaveDAOFactoryImpl;
import dto.LeaveDTO;
import dto.LeaveSearchDTO;

/**
 * Servlet implementation class MovementRegister
 */
@WebServlet("/MovementRegister")
public class MovementRegister extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private static int move = 0;   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MovementRegister() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    
  protected void doProcess(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
  {
	  
	  boolean flagList=false;
	  
		Date dateFrom;
		Date dateTo;
		
		Calendar calendar1 = Calendar.getInstance();
		Calendar calendar2 = Calendar.getInstance();
		int year=calendar1.get(Calendar.YEAR);
		//int year2=calendar1.get(Calendar.YEAR);
		int month1=calendar1.get(Calendar.MONTH);
		int month2=calendar1.get(Calendar.MONTH);
		//int day=calendar1.get(Calendar.DATE);
		//if (month1==12)
		//{
		//	month1=1;
		//	year1=year1+1;
		//}
		//else if(month2==1)
		//{
		//	month2=12;
		//	year2=year2-1;
			
		//}
		
		
		
		if(request.getParameter("preMonth")!=null)
		{
			move-=1;
			if(month1==0)
			{
				month1=11;
				year=year-1;
				
			}
			
		}
		if(request.getParameter("curMonth")!=null)
		{
			move=0;
			
		}
		if(request.getParameter("nexMonth")!=null)
		{
			move+=1;
			if (month1==11)
			{
				month1=0;
				year=year+1;
			}
			
		}
		//request.setAttribute("move", move);
		//remember=move;
		
		
		
//		System.out.println(year);
//		System.out.println(month1);		
		calendar1.set(year,month1+move,1);
		calendar2.set(year,month1+move,31);
		
		//calendar1.set(2008,1,1);
		//calendar2.set(2012,1,1);
		
		dateFrom=calendar1.getTime();
		dateTo=calendar2.getTime();
		
		LeaveSearchDTO ldto = new LeaveSearchDTO();
		ldto.set_startDate(dateFrom);
		ldto.set_endDate(dateTo);
		//ldto.set_searchByAppDate(false);
		ldto.set_ApprovingmanID(0);
		
		LeaveDAOFactoryImpl fac = new LeaveDAOFactoryImpl();
		LeaveDAO dao = fac.getLeaveDAO();
		ArrayList<LeaveDTO> dtoList = null;
		try {
			dtoList = dao.findLeaveByCriteria(ldto);

		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Iterator<LeaveDTO> it=dtoList.iterator();
		
		if(!it.hasNext())
		{
			dtoList=null;
		}
		
		
		if (dtoList!=null) {
			NewLeaveList nlist = new NewLeaveList();
			ArrayList<LeaveDTO> listSort = nlist.getSortList(dtoList);
			request.setAttribute("LeaveInfo", listSort);
			
			flagList=true;
			request.setAttribute("flagList", flagList);

			
		}
		
		boolean flagList0=true;
		if(flagList)
		{
			flagList0=false;
		}
		request.setAttribute("flagList0", flagList0);
		RequestDispatcher rd=request.getRequestDispatcher("/MovementRegister.jsp");
		rd.forward(request, response);
  }

    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request, response);
		
	}
//12
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request, response);
	}
	
	
	

}
